﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogicLayer.Base;
using DALLayer.Providers;
using DataDefinition;
using Objects;

namespace BusinessLogicLayer.Manager
{
    public class TagManager : ManagerBase<TagDataProvider>
    {
        #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="TagManager" /> class.
        /// </summary>
        public TagManager() { } 
        #endregion

        #region [Manager base class implementations]
        /// <summary>
        /// Pass the data provider to the class
        /// </summary>
        protected override void InitializeDataProvider()
        {
            //Pass the specific data provider to the manager class
            base.DataProvider = new TagDataProvider();
        }

        #endregion

        #region [Public Methods]
        public List<Tag> UpdateAllTagsScalar(List<Tag> tagList)
        {

            List<Tag> tagDetails = new List<Tag>();
            Tag addedTag = null;
            try
            {
                foreach (Tag tag in tagList)
                {
                    //Insert an tag
                    int identity = base.DataProvider.InsertTagScalar(tag.TagName, tag.IsActive);
                    if (identity > 0)
                    {
                        addedTag = this.GetTags(identity, true)[0];
                        tagDetails.Add(addedTag);
                    }
                }

            }
            catch (Exception eX)
            {
                // log the error here
            }
            //returns tagDetails
            return tagDetails;
        }

        /// <summary>
        /// Gets all the tags
        /// </summary>
        /// <returns>tag list</returns>
        public List<Tag> GetTags()
        {
            //The tag details
            List<Tag> tagDetails = null;
            try
            {
                //Get the tag details list
                tagDetails = this.GetTags(0, false);
            }
            catch (Exception eX)
            {
                //log error here
            }
            //The user details list to return
            return tagDetails;
        } 
        #endregion

        #region [Private Methods]
        /// <summary>
        /// The method that gets the list of tags or just one tag
        /// </summary>
        /// <param name="tagId">tag id</param>
        /// <param name="selectById">The check to see if select by id or not</param>
        /// <returns>tag list</returns>
        private List<Tag> GetTags(int tagId, bool selectById)
        {
            //The tag details to be returned
            List<Tag> tagDetails = null;
            try
            {
                //Get the data set returned from the data provider
                AutoFixDataSet dataSet = base.DataProvider.GetAllTags(tagId, selectById);

                if (dataSet != null && dataSet.Tag != null && dataSet.Tag.Rows != null)
                {
                    //Set the returned filled list
                    tagDetails = FillTagObjectsByEntities(dataSet.Tag);
                }
            }
            catch (Exception eX)
            {
                //log the error here, dont throw the exception
            }
            //Return the user details list
            return tagDetails;

        }
        /// <summary>
        /// Fill the tag object
        /// </summary>
        /// <param name="dataTable">data table</param>
        /// <returns>the tag list</returns>
        private List<Tag> FillTagObjectsByEntities(AutoFixDataSet.TagDataTable dataTable)
        {
            //The tag list that contains the objects
            //Loop through the data table and create new TagObject 
            //finally select the TagObject and convert to list
            List<Tag> tagDetails = (from to in
                                        (from t in dataTable.AsEnumerable<AutoFixDataSet.TagRow>()
                                         select new
                                         {
                                             TagObject = new Tag()
                                             {
                                                 TagID = t.TagID,
                                                 TagName = t.Tag,
                                                 IsActive = t.IsActive
                                             }
                                         })
                                    select to.TagObject).ToList();
            //Return the filled user details list
            return tagDetails;
        }


        #endregion
    }
}
